﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class Tailor_Measurement : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            loaddata();
        }
    }

    private void loaddata()
    {

        string sql = @"
SELECT [PictureID] as ID
      ,[PictureName] as Name 
,ProductName ,PictureDetails
  FROM Tailor_Picture inner join Tailor_Product on Tailor_Product.ProductID =Tailor_Picture.ProductID
order by ProductName,[PictureName]

Select ProductID,ProductName from Tailor_Product
";

        DataSet ds = CommonManager.SQLExec(sql);
        gvItems.DataSource = ds.Tables[0];
        gvItems.DataBind();

        ddlProduct.Items.Clear();
        //ddlProduct.Items.Add(new ListItem("Select Product","0"));
        ddlProduct.DataSource = ds.Tables[1];
        ddlProduct.DataTextField = "ProductName";
        ddlProduct.DataValueField = "ProductID";
        ddlProduct.DataBind();
    }

    protected void lbSelect_Click(object sender, EventArgs e)
    {
        LinkButton linkButton = new LinkButton();
        linkButton = (LinkButton)sender;
        
        string sql = @"
SELECT [PictureID] as ID
      ,[PictureName] as Name 
,ProductID,PictureDetails
  FROM Tailor_Picture  where [PictureID]=" + linkButton.CommandArgument;
        DataSet ds = CommonManager.SQLExec(sql);

        hfID.Value = ds.Tables[0].Rows[0][0].ToString();
        txtName.Text = ds.Tables[0].Rows[0][1].ToString();
        ddlProduct.SelectedValue = ds.Tables[0].Rows[0][2].ToString();
        txtPicture.Text = ds.Tables[0].Rows[0][3].ToString();
    }
    protected void lbDelete_Click(object sender, EventArgs e)
    {
        LinkButton linkButton = new LinkButton();
        linkButton = (LinkButton)sender;
        
        string sql = @"Delete [Tailor_Picture]   where [PictureID]=" + linkButton.CommandArgument;
         
        CommonManager.SQLExec(sql);
        loaddata();
        
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        string sql="";
        if (hfID.Value == "")
        {
            sql = @"
INSERT INTO [Tailor_Picture]
           ([PictureName],ProductID,PictureDetails
           )
     VALUES(N'" + txtName.Text + @"'," + ddlProduct.SelectedValue + @",N'" + txtPicture.Text + @"')
";
        }
        else
        {
            sql = "update [Tailor_Picture] set [PictureName]=N'" + txtName.Text + @"',[PictureDetails]=N'" + txtPicture.Text + @"',ProductID=" + ddlProduct.SelectedValue + @"  where [PictureID]=" + hfID.Value;
            hfID.Value = "";
        }
        txtName.Text = "";
        txtPicture.Text = "";
        
        CommonManager.SQLExec(sql);
        loaddata();
    }
    protected void ddlItem_SelectedIndexChanged(object sender, EventArgs e)
    {
        loaddata();
    }
}